package com.hzcx.app.simplechat.api;

import android.content.Context;


import com.hzcx.app.simplechat.MyApplication;
import com.hzcx.app.simplechat.base.BaseLoadingDialog;
import com.hzcx.app.simplechat.util.net.NetWorkUtils;

import io.reactivex.disposables.Disposable;


/**
 *  带dialog的网络返回请求统一处理
 * @param <T>
 */
public abstract class BaseDialogObserver<T> extends BaseObserver<T>{

    private Context context;
    private String msg;
    private Disposable disposable;
    private BaseLoadingDialog loadingDialog;
    private boolean isShow=true;

    public BaseDialogObserver(Context context){
        this.context=context;
    }

    public BaseDialogObserver(Context context, String msg) {
        this.context = context;
        this.msg = msg;
    }

    public BaseDialogObserver(Context context,boolean isShow){
        this.context=context;
        this.isShow=isShow;
    }

    @Override
    public void onSubscribe(Disposable d) {
        super.onSubscribe(d);
        disposable=d;
        try {
            if (NetWorkUtils.isConnted(MyApplication.getContext())&&isShow) {
                loadingDialog=new BaseLoadingDialog(context);
                loadingDialog.show();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onError(Throwable e) {
        super.onError(e);
        if(disposable.isDisposed()){
            disposable.dispose();
        }
        try {
            if(loadingDialog!=null&&loadingDialog.isShowing()){
                loadingDialog.dismiss();
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }

    @Override
    public void onComplete() {
        if(disposable.isDisposed()){
            disposable.dispose();
        }
        try {
            if(loadingDialog!=null&&loadingDialog.isShowing()){
                loadingDialog.dismiss();
            }
        }catch (Exception ex){
            ex.printStackTrace();
        }
    }
}
